<template>
	<view class="container">
		<!-- 顶部标题栏 -->
		<view class="header">
			<view class="header-title">在线设备</view>
		</view>

		<!-- 筛选区域 -->
		<view class="filter-bar">
			<!-- 使用 uni-data-select 美观的下拉框 -->
			<view class="picker-group">
				<uni-data-select v-model="areaSelected" :localdata="areaOptions" placeholder="请选择区域" />
			</view>
		</view>

		<!-- ATM设备列表 -->
		<view>
			<view v-for="atm in filteredList" :key="atm.id" class="atm-card" @click="goToDetail(atm)">
				<view class="card-header">
					<text class="atm-title">{{ atm.code }}</text>
					<text class="status online">🟢 运行中</text>
				</view>
				<view class="card-body">
					<view class="left">
						<image class="atm-image" :src="atm.image" mode="aspectFill" @click="goToDetail(atm)" />
						<view class="card-info">
							<text>区域：{{ atm.areaName }}</text>
							<text>库存金额：￥{{ atm.stock }}</text>
						</view>
					</view>
					<view class="right">
						<button class="detail-btn" @click="goToDetail(atm)">详细信息</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAtms,
	} from '@/api/atm';
	import {
		getAreas,
		getAreaById
	} from '@/api/area';
	import {
		getManagersByAreaId
	} from '@/api/user';
	export default {
		data() {
			return {
				devices: [],
				// 下拉框数据格式必须为 { value, text }
				areaOptions: [{
					value: null,
					text: ''
				}],
				areaSelected: '',
				activeButton: null
			}
		},

		computed: {
			filteredList() {
				return this.devices.filter(item => {
					return (
						(!this.areaSelected || item.areaId === this.areaSelected)
					)
				})
			}
		},

		onLoad: async function() {
			await this.fetchData();
		},

		methods: {
			async fetchData() {
				this.loading = true;
				const areaList = await getAreas();
				console.log(areaList);
				this.areaOptions = areaList.map(area => ({
					value: area.id,
					text: area.name
				}));

				const result = await getAtms();
				if (result.code === 200) {
					const deviceList = result.data;
					this.devices = deviceList.map(d => ({
						id: d.id,
						code: d.atmCode,
						status: d.status,
						stock: d.money,
						areaId: d.areaId,
						areaName: this.getAreaNameById(d.areaId),
						image: '/static/ATM_pic.png',
						manager: '张三',
					}));
					this.loading = false;
				}
			},

			goToDetail(atm) {
				uni.navigateTo({
					url: `/pages/deviceManagement/deviceDetails?id=${atm.id}`
				})
			},

			getAreaNameById(areaId) {
				const areaList = uni.getStorageSync('areaDatas');
				let result;
				areaList.forEach(area => {
					if (result) return;
					if (area.id === areaId) {
						result = area.name;
					}
				});
				return result;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #f8f8f8;
		min-height: 100vh;
	}

	.header {
		background-color: #BD3124;
		padding: 0 30rpx 20rpx;
		color: #fff;
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
	}

	.filter-bar {
		background-color: #fff;
		margin: 20rpx;
		padding: 24rpx;
		border-radius: 20rpx;
		// box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	}

	.picker-group {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin-bottom: 30rpx;
	}

	.button-bar {
		display: flex;
		justify-content: space-around;
	}

	.btn {
		flex: 1;
		margin: 0 10rpx;
		padding: 20rpx;
		font-size: 26rpx;
		border-radius: 12rpx;
		background-color: #eee;
		color: #333;
		text-align: center;
	}

	.btn.active {
		background-color: #BD3124;
		color: #fff;
	}

	.atm-card {
		background: #fff;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 24rpx;
		// box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
		font-weight: 600;
	}

	.card-header .atm-title {
		font-size: 36rpx;
	}

	.online {
		color: #28a745;
	}

	.offline {
		color: #dc3545;
	}

	.card-body {
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
	}

	.atm-image {
		width: 120px;
		height: 120px;
		margin: 20rpx 0;
		border-radius: 12rpx;
		object-fit: contain;
	}

	.card-info text {
		display: block;
		font-size: 26rpx;
		margin-bottom: 6rpx;
		color: #333;
	}

	.card-body .right {
		display: flex;
		justify-content: flex-end;
	}

	.detail-btn {
		margin-top: 20rpx;
		margin-right: 0;
		width: 80px;
		height: 28px;
		line-height: 27px;
		background-color: #FFF;
		text-align: center;
		border-radius: 8px;
		font-size: 12px;
		color: #D20000;
		border: 0.5px solid #D20000;
	}

	.detail-btn::after {
		display: none;
	}
</style>